package com.itheima.controller;

import com.itheima.dto.MessageConstant;

import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.dto.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 预约管理-套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除套餐
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("删除套餐，id：{}", id);
        setmealService.deleteById(id);
        return Result.success("删除套餐成功");
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询套餐，id：{}", id);
        Setmeal setmeal = setmealService.getById(id);
        return Result.success(setmeal, "查询套餐数据成功");
    }

    /**
     * 根据套餐id查询对应的检查组id集合
     *
     * @param id
     * @return
     */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result getCheckgroupById( Integer id) {
        log.info("查询套餐id：{}，对应的检查组id", id);
        List<Integer> ids = setmealService.getCheckgroupById(id);
        return Result.success(ids, "查询套餐成功");
    }

    /**
     * 编辑套餐
     *
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Setmeal setmeal, @RequestParam List<Integer> checkgroupIds) {
        setmealService.update(checkgroupIds, setmeal);
        return Result.success("编辑套餐成功");
    }



    @RequestMapping("/findPage")
    public PageResult getSetmealByPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping()
    public Result addSetmeal(@RequestBody Setmeal setmeal,@RequestParam List<Integer> checkgroupIds) {
        setmealService.insertSetmeal(setmeal,checkgroupIds);
        return Result.success(MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @RequestMapping("/upload")
    public Result uploadPicture(MultipartFile imgFile) throws IOException {

        //原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        //获取原文件名字的末尾
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //使用UUid 命名 反正名字重复  覆盖文件
        String fileName = UUID.randomUUID().toString() + suffix;
        //创建一个目录对象

        byte[] bytes = imgFile.getBytes();
        QiniuUtils.upload2Qiniu(bytes, fileName);
        return Result.success(fileName, MessageConstant.PIC_UPLOAD_SUCCESS);

    }

}


